package RoomManagement

import (
	"MedicalTreatment/global"
	"MedicalTreatment/model"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
)

func RoomIncrease(r *gin.Engine) {
	r.POST("/roomAdd", func(context *gin.Context) {
		var room model.Room
		if err := context.ShouldBindJSON(&room); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		if err := global.DB.Create(&room).Error; err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		for i := 0; i < room.NumberBeds; i++ {
			bed := model.Bed{
				Model:       gorm.Model{},
				Name:        room.Name + "-" + strconv.Itoa(i),
				PatientId:   0,
				PatientName: "",
				RoomId:      room.ID,
			}
			if err := global.DB.Create(&bed).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
				return
			}

		}
		context.JSON(200, gin.H{
			"Msg": "创建成功",
		})
	})
}
